package com.ruoyi.web.controller.document;

import java.util.ArrayList;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.page.R;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.document.domain.Contract;
import com.ruoyi.document.service.IContractService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 合同签约管理Controller
 * 
 * @author ruoyi
 * @date 2023-03-03
 */
@RestController
@RequestMapping("/document/contract")
public class ContractController extends BaseController
{
    @Autowired
    private IContractService contractService;


   static R r = new R();

    /**
     * 查询合同签约管理列表
     */
    @PreAuthorize("@ss.hasPermi('document:contract:list')")
    @GetMapping("/list")
    public TableDataInfo list(Contract contract)
    {
        startPage();
        List<Contract> list = contractService.selectContractList(contract);
        return getDataTable(list);
    }

    /**
     * 导出合同签约管理列表
     */
    @PreAuthorize("@ss.hasPermi('document:contract:export')")
    @Log(title = "合同签约管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Contract contract)
    {
        List<Contract> list = contractService.selectContractList(contract);
        ExcelUtil<Contract> util = new ExcelUtil<Contract>(Contract.class);
        util.exportExcel(response, list, "合同签约管理数据");
    }

    /**
     * 获取合同签约管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('document:contract:query')")
    @GetMapping(value = "/{conId}")
    public AjaxResult getInfo(@PathVariable("conId") Integer conId)
    {
        return success(contractService.selectContractByConId(conId));
    }

    /**
     * 新增合同签约管理
     */
    @PreAuthorize("@ss.hasPermi('document:contract:add')")
    @Log(title = "合同签约管理", businessType = BusinessType.INSERT)
    @PostMapping
    public boolean add(@RequestBody Contract contract)
    {
        //return toAjax(contractService.insertContract(contract));
        return contractService.insertContract(contract);
    }

    /**
     * 修改合同签约管理
     */
    @PreAuthorize("@ss.hasPermi('document:contract:edit')")
    @Log(title = "合同签约管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public boolean edit(@RequestBody Contract contract)
    {
       // return toAjax(contractService.updateContract(contract));
        return contractService.updateContract(contract);
    }

    /**
     * 删除合同签约管理
     */
    @PreAuthorize("@ss.hasPermi('document:contract:remove')")
    @Log(title = "合同签约管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{conIds}")
    public Boolean remove(@PathVariable Integer[] conIds)
    {
        for (Integer conId : conIds) {
            r.addList(conId);
        }
        return contractService.deleteContractByConIds(conIds);
    }
    /**
     * 回滚
     */
    @PreAuthorize("@ss.hasPermi('personnel:employee:rollback')")
    @Log(title = "合同管理回滚", businessType = BusinessType.DELETE)
    @DeleteMapping
    public Boolean rollback()
    {
        List list = r.getList();

        if (list.size()==0){
            return false;
        }else {
            Object[] array = list.toArray();
            contractService.rollbackEmployee(array);
            r.setList(new ArrayList<>());
            return true;
        }
    }

    /**
     * 修改合同签约管理
     */
    @PreAuthorize("@ss.hasPermi('document:contract:edit')")
    @Log(title = "合同签约管理", businessType = BusinessType.UPDATE)
    @PutMapping("/{conId}")
    public AjaxResult editById(@PathVariable("conId") Integer conId)
    {
        return toAjax(contractService.updateContractById(conId));
    }
}
